<template>
  <forms ref="forms" :config="config" @submit:forms="onsubmit" :btns="btns">
    <template v-slot:config_form="{row}">
      <text_more :name="row.name" v-model:value="row.value" :placeholder="row.placeholder"></text_more>
    </template>
  </forms>
</template>

<script>
import {ajax} from "@/js/api_tool.js";
import {field_type, iframe} from "@/js/tool.js";
import s94Mjs from "s94-js";
import {_config} from "@/js/s94_tool.js";

export default {
  props: ['__iframe'],
  data(){
    let id = this.$get('id'), btns = undefined;
    let type_map = field_type(field_type.CAN_CONFIG, 'label');
    let form_param_config = {
      name:{label:'参数名',name:'name',type:'text_one',config:'',tips:'',value:'', width:'160px',validate:['required']},
      label:{label:'参数别名',name:'label',type:'text_one',config:'',tips:'',value:'', width:'140px',validate:['required']},
      tips:{label:'提示信息',name:'tips',type:'text_one',config:'',tips:'',value:'', width:'160px'},
      type:{label:'表单组件',name:'type',type:'select_one',config:type_map,tips:'',value:'', width:'100px',validate:['required']},
      config:{label:'组件配置',name:'config',type:'text_more',config:'',tips:'',value:'', list_hide:0},
    };
    let forms_config = {
      parent_id: {label:'所属组',name:'parent_id',type:'select_one',config:[],tips:'配置所属分组',value:'0',can_select_parent:true},
      name: {label:'配置名',name:'name',type:'text_one',tips:'配置名，字母加数字，获取配置的依据，谨慎命名，不建议随意修改'},
      title: {label:'标题',name:'title',type:'text_one',tips:''},
      info: {label:'简介',name:'info',type:'text_more',tips:''},
      is_only: {label:'是否唯一',name:'is_only',type:'radio_switch',config:'',tips:'唯一配置只能在总站显示和操作，并且所有租户下通用',value:0},
      is_inherit: {label:'是否继承',name:'is_inherit',type:'radio_switch',config:'',tips:'继承配置在租户设定为空时，会使用总站的配置',value:1},
      is_private: {label:'是否隐藏',name:'is_private',type:'radio_switch',config:'',tips:'对于隐藏配置，不会公开显示，查看和编辑都需要验证管理员密码',value:0},
      need_right: {label:'权限需求',name:'need_right',type:'select_more',config:[],tips:'如果管理员没有对应的权限(全部)，则该组配置不会对其开放'},
      sort: {label:'排序',name:'sort',type:'text_one',config:'number',tips:'',value:50},

      form_param_json: {label:'表单配置',name:'form_param_json',type:'json_param_arr',config:form_param_config,tips:'',value:'[]'},
    }
    if (id){
      forms_config.create_time = {label:'创建时间',name:'create_time',tips:''};
    }
    if (this.$get('is_info')){
      s94Mjs.each(forms_config, function (row){ row.is_info = true; });
      btns = [];
    }else {
      if (_config('this_tenant.id') > 0){
        forms_config.is_only.remove = true;
        forms_config.is_inherit.remove = true;
      }
    }
    return {
      id: id,
      btns: btns,
      config: forms_config,
    }
  },
  methods: {
    onsubmit(formData){
      let url = this.id ? `/admin/config_group/edit?id=`+this.id : '/admin/config_group/add';
      ajax({url:url, method:'POST', data:formData}, (res)=>{
        if (this.__iframe){
          iframe.exe('config_group/index', 'init');
          iframe.hide('config_group/edit');
          if (!this.id) this.$refs['forms'].init();
        }else {
          this.$router.push('index');
        }
      }, (res)=>{
        if (res.errors) this.$refs['forms'].errors = res.errors;
      })
    },
    set_remove(){
      this.config.is_inherit.remove = parseInt(this.config.is_only.value)===1;
      // this.config.is_show.remove = this.config.is_inherit.remove || parseInt(this.config.is_inherit.value)===0;
    }
  },
  watch: {
    'config.form_param_json.config.type.value'(v){
      s94Mjs.each(field_type.component_config(v), (v,k)=>{
        this.config.form_param_json.config.config[k] = v;
      })
    },
    'config.is_only.value'(v, oldV){
      this.set_remove();
    },
    'config.is_inherit.value'(v, oldV){
      this.set_remove();
    },
  },
  mounted() {
    let app = this;
    if (!this.__iframe) window['app'] = this;
    if (this.id) {
      ajax({url:'/admin/config_group/info?id='+this.id, cache:true}, (res)=>{
        this.$refs['forms'].init(res.data);
        return false;
      }, ()=>{
        if (this.__iframe){
          iframe.remove('edit');
        }else {
          this.$router.push('index');
        }
      })
    }

    //其他初始化操作
    let query = '';
    ajax({url:'/admin/config_group/list?'+query,cache:true}, (res)=>{
      //{children_key:'子项键值名称，默认为children', content_key:'选项文本字段', value_key:'选项值字段', index_key:'索引字段，和parent联合确定子项', parent_key:'指向父级索引数据的字段'}
      let config = this.decode_config(res.data.list, {
        content_key: 'title',
        value_key: 'id',
        index_key: 'id',
        parent_key: 'parent_id'
      });
      config.unshift({content:'顶级配置', value:'0'});
      app.config.parent_id.config = config;
      return false
    })
    ajax({url:'/admin/admin_role/rights',cache:true},(res)=>{
      this.config.need_right.config=res.data;
      return false
    })
  }
}
</script>
